import { useEffect } from "react";
import { useRoutes, useLocation, useNavigate } from "react-router-dom";
import router from "@/router";
import { ValidateToken } from "./api";
function ToHome() {
  const navigate = useNavigate();
  useEffect(() => {
    navigate("/page1");
  }, []);
  return <></>;
}
function ToLogin() {
  const navigate = useNavigate();
  useEffect(() => {
    navigate("/login");
  }, []);
  return <></>;
}
function BeforeRouterEnter() {
  const Outlet = useRoutes(router);
  let location = useLocation();
  let token = localStorage.getItem("token");
  if (location.pathname === "/login" && token) {
    return <ToHome />;
  }
  if (location.pathname !== "/login" && !token) {
    return <ToLogin />;
  }
  return Outlet;
}

const App = () => {

  return (
    <>
      <BeforeRouterEnter />
    </>
  );
};

export default App;
